// pages/choosePackage/choosePackage.js
const DB = wx.cloud.database()
var util = require('../../utils/util.js')
var that;
Page({
  data: {
    list:[1,2,3],
    tab:2,
  },
  onLoad: function (options) {
    that=this;
    this.get_b()
    this.get_a()
  },
  nav_user_detail(e){
    var item = e.currentTarget.dataset.item
    var user_openid = item.a == wx.getStorageSync('userInfo')._openid?item.b:item.a

    console.log('user_openid',user_openid)
    DB.collection('user').where({
      _openid: user_openid,
    })
    .get().then(ress => {
      var userInfo= ress.data[0]
      console.log("userInfo",userInfo)
      wx.setStorageSync('seeuserInfo', userInfo)
      wx.navigateTo({
        url: '/pages/user_detail/user_detail',
      })
    })
  },
  get_a(){
    var openid = wx.getStorageSync('userInfo').openid
    DB.collection('invitation_list').where({
      a: openid,
    })
    .get().then(ress => {
      console.log('我邀请的',openid,ress)
      ress.data.forEach(item=>{
        item.status = this.zhuangtai(item.status)
      })
      this.setData({
        aaa_list:ress.data
      })
    })
  },
  get_b(){
    var openid = wx.getStorageSync('userInfo').openid
    DB.collection('invitation_list').where({
      b: openid,
    })
    .get().then(ress => {
      console.log('呗邀请的',openid,ress)
      ress.data.forEach(item=>{
        item.status = this.zhuangtai(item.status)
      })
      this.setData({
        bbb_list:ress.data
      })
    })
  },
  zhuangtai(status){
    if(status==1){
      return '已同意'
    }else if(status ==2){
      return '已拒绝'
    }else{
      return '待确认'
    }
  },
  tongyi(e){
    var item =e.currentTarget.dataset.item;
    console.log('id',item._id)
    DB.collection('invitation_list').doc(item._id).update({
      data: {
        status: 1
      },
      success: function (res) {
        console.log('2222222222')
        that.get_b()
        that.jieshou(item)
      }
    })
  },
  
  jieshou(item) {
    var date = util.formatTime(new Date())
    var {a,a_img,a_name,b,b_img,b_name} = item;
    let data = {
      a,a_img,a_name,b,b_img,b_name,date,msgList:[],a_zaixian:false,b_zaixian:false
    }
    console.log('user——————————', data)
    DB.collection('friends').add({
      data: data,
      success: function (res) {
        wx.reLaunch({
          url: '/pages/friends_list/friends_list' // 这里是要跳转的路径
        })
      }
    })
  },
  jujue(e){
    var item =e.currentTarget.dataset.item;
    console.log('id',item._id)
    DB.collection('invitation_list').doc(item._id).update({
      data: {
        status: 2
      },
      success: function (res) {
        that.get_b()
      }
    })
  },
  tab(e){
    console.log('taiget',e)
    this.setData({
      tab:e.currentTarget.id
    })
  },
  nav_upInfo(){
    wx.navigateTo({
      url: '../upInfo/upInfo',
    })
  },
})